﻿using Engine.Result;

using Scheduler;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Engine
{
    public class RunMachine
    {
        private Scheduler.Scheduler _scheduler;
        private WaferTesterContext _ctx;
        public event EventHandler<ChipResult> OnPassDone;
        private List<(int, int)> _listValue;
        public int Index;
        public RunMachine(params object[] userData)
        {
            _scheduler = new SchedulerBuilder().BuildScheduler(userData);
            _ctx = (WaferTesterContext)userData[0];

             var str = System.IO.File.ReadAllLines(Environment.CurrentDirectory + @"\Middlewares\PositionTest.txt");
            _listValue = new List<(int, int)>();
            var i = 0;
            foreach (var item in str)
            {
                i++;
             
                var tmp = item.Split(',');
                var x = int.Parse(tmp[0]);
                var y = int.Parse(tmp[1]);

                _listValue.Add((x, y));

                _ctx.Result.AddLotChip(new DUTPoint(x, y));
            }
        }

     

        public Task Run()
        {
            Debug.Assert(_scheduler != null);
            //_ctx = new MachineContext();
            _ctx.UserCancelled = false;
            return _scheduler.Execute(_ctx);
        }

        public void Stop()
        {
            _ctx.UserCancelled = true;
        }

    }
}
